# -*- coding: utf-8 -*-
__author__ = 'niimailtah'

from PyQt4.QtCore import *
from pluginspec import *

class PluginSpec:
    Invalid, Read, Resolved, Loaded, Initialized, Running, Stopped, Deleted = 0, 1, 2, 3, 4, 5, 6, 7

    def __init__(self, name = None):
        self._disabledByDefault = False
        self._enabled = True
        self._state = self.Invalid
        self._hasError = False
        self._name = name

    def name(self):
        return self._name

    def version(self):
        return self._version

    def description(self):
        return self._description

    def isDisabledByDefault(self):
        return self._disabledByDefault

    def isEnabled(self):
        return self._enabled

    def setEnabled(self, value):
        self.enabled = value

    def setDisabledByDefault(self, value):
        self._disabledByDefault = value

    def state(self):
        return self._state

    def hasError(self):
        return self._hasError

    def errorString(self):
        return self._errorString

    def initializePlugin(self):
        pass

    def read(self, fileName):
        self._state = self.Invalid
        self._hasError = False
        _file = QFile(fileName)
        _file.open(QIODevice.ReadOnly)
        reader = QXmlStreamReader(_file);
        while not reader.atEnd():
            reader.readNext()
            if reader.tokenType() == QXmlStreamReader.StartElement:
                self.readPluginSpec(reader)
                break
        if reader.hasError():
            pass
        self._state = PluginSpec.Read
        return True

    def readPluginSpec(self, reader):
        element = reader.name()
        if element != 'plugin':
            return
        self._name = reader.attributes().value('name')
        self._version = reader.attributes().value('version')
